package com.tokenmaster.aitrade.trade;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;

import org.apache.log4j.Logger;

import com.tokenmaster.aitrade.common.system.KLinePoint;
import com.tokenmaster.aitrade.data.SystemDB;
import com.tokenmaster.aitrade.market.okex.IOkex_Parameter.Types_Enum;
import com.tokenmaster.aitrade.market.okex.Market_Okex_Spot;
import com.tokenmaster.aitrade.trade.boll.Index_Boll_Info;
import com.tokenmaster.aitrade.trade.boll.Index_boll;
import com.tokenmaster.aitrade.trade.kdj.Index_KDJ;
import com.tokenmaster.aitrade.trade.kdj.Index_KDJ_Info;

/**
 * @ClassName: KLinesManager
 * @Description: K线管理器,生成指标
 * @author buxinyun
 * @date 2018年8月24日 上午2:23:20
 * 
 */
public class KLinesManager {

	private Market_Okex_Spot market_Okex_Spot = Market_Okex_Spot.getInstance();

	private SystemDB systemDB = SystemDB.getInstance();

	private Logger logger = Logger.getLogger(this.getClass());

	private Index_KDJ index_KDJ = new Index_KDJ();

	private Index_boll index_boll = new Index_boll();

	private SimpleDateFormat sformat = new SimpleDateFormat("YYYY-MM-dd HH:mm:ss");

	public void checkNewKLine(String symbol, Types_Enum types_Enum) throws Exception {

		// 从OK获取 K线
		List<KLinePoint> kLinePoints = market_Okex_Spot.getKLines(symbol, types_Enum.getKeyWord());

		// 获取最后一条K线
		KLinePoint lastKlinePoint = systemDB.getLastKLinePoint(symbol, types_Enum.getKeyWord());

		for (int i = 0; i < kLinePoints.size(); i++) {

			KLinePoint current_kPoint = kLinePoints.get(i);

			// 第一条K线
			if (lastKlinePoint == null) {
				systemDB.insertKLine(current_kPoint);
				String timestr = sformat.format(current_kPoint.getUnixTime());
				logger.info("first " + types_Enum.getKeyWord() + " KLine " + timestr);

				cal_IndexKdj(current_kPoint);
				cal_IndexBoll(current_kPoint);

			} else if (current_kPoint.getUnixTime() == lastKlinePoint.getUnixTime()) {

				// 存在K线记录则更新
				systemDB.updateKLine(current_kPoint);

				logger.info(
						"Update " + types_Enum.getKeyWord() + " KLine " + sformat.format(current_kPoint.getUnixTime()));

				cal_IndexKdj(current_kPoint);
				cal_IndexBoll(current_kPoint);

			} else if (current_kPoint.getUnixTime() > lastKlinePoint.getUnixTime()) {

				systemDB.insertKLine(current_kPoint);
				String timestr = sformat.format(current_kPoint.getUnixTime());
				logger.info("New " + types_Enum.getKeyWord() + " KLine " + timestr);

				cal_IndexKdj(current_kPoint);
				cal_IndexBoll(current_kPoint);
			}
		}

	}

	/**
	 * 计算KDJ
	 */
	private void cal_IndexKdj(KLinePoint kLinePoint) {

		// 计算某个点的KDJ,并保存到数据库
		Index_KDJ_Info index_KDJ_Info = index_KDJ.calculation(kLinePoint);

		systemDB.updateIndexKDJInfo(index_KDJ_Info);
	}

	/**
	 * 计算Boll
	 * 
	 * @param kLinePoint
	 */
	private void cal_IndexBoll(KLinePoint kLinePoint) {

		// 计算某个点的 boll
		Index_Boll_Info index_Boll_Info = index_boll.calculation(kLinePoint);

		if (systemDB.getIndexBollInfo(kLinePoint.getSymbol(), kLinePoint.getType(), kLinePoint.getUnixTime()) == null) {
			systemDB.insertIndexBollInfo(kLinePoint.getSymbol(), kLinePoint.getType(), index_Boll_Info);
			logger.info("Insert New Boll" + sformat.format(kLinePoint.getUnixTime()));
		} else {
			systemDB.updateIndexBollInfo(index_Boll_Info);
			logger.info("Update Boll " + sformat.format(kLinePoint.getUnixTime()));
		}

	}

}
